package com.yangxk.kadmin.web.common.interceptor;

import com.yangxk.kadmin.common.exception.ParamException;
import com.yangxk.kadmin.web.common.annotation.DemoModel;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 演示模式注解拦截器
 *
 * @author yangxk
 * @date 2018/11/2 15:01
 */
public class DemoModelInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod))
            return true;

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        DemoModel annotation = handlerMethod.getMethod().getAnnotation(DemoModel.class);

        if (annotation == null)
            return true;

        throw new ParamException("演示模式, 不允许操作");
    }
}
